﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Comm
{
    public class PageBase:System.Web.UI.Page
    {

        /// <summary>
        /// 删除文件
        /// </summary>
        /// <param name="Path"></param>
        protected void DeleteFile(string Path)
        {
            string FilePath = Server.MapPath(Path);
            if (System.IO.File.Exists(FilePath))
            {
                System.IO.File.Delete(FilePath);
            }
        }

        //价格格式化
        protected string NumFormat(string str)
        {
            return float.Parse(str.Trim()).ToString("C");
        }

        /// <summary>
        /// 價格格式化,保留2位,不夠加“0”
        /// </summary>
        /// <param name="Str"></param>
        /// <returns></returns>
        protected string Getprice(string Str)
        {
            decimal price = Convert.ToDecimal(Str);
            if (Decimal.Round(price, 2).ToString().IndexOf(".") < 0)
            {
                return Convert.ToDecimal(Decimal.Round(price, 2).ToString() + ".00").ToString();
            }
            else
            {
                return Decimal.Round(price, 2).ToString();
            }
        }

        /// <summary>
        /// 格式化时间
        /// </summary>
        /// <param name="str">要格式化的字符串</param>
        /// <param name="type">格式类型(0：yyyy-MM-dd HH:mm:ss,1：yyyy-MM-dd)</param>
        /// <returns></returns>
        protected string FormatDate(string str, int type)
        {
            switch (type)
            {
                case 0: return DateTime.Parse(str.Trim()).ToString("yyyy-MM-dd HH:mm:ss");
                case 1: return DateTime.Parse(str.Trim()).ToString("yyyy-MM-dd");
                default: return DateTime.Parse(str.Trim()).ToString("yyyy-MM-dd HH:mm:ss");
            }
        }

        /// <summary>
        /// 获取当前页
        /// </summary>
        protected int PageIndex
        {
            get { return QueryInt("page"); }
        }

        /// <summary>
        /// 获取地址栏Int型参数
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        protected int QueryInt(string name)
        {
            int _oid = -1;
            string _id = Request.QueryString[name];
            if (_id == null || !int.TryParse(_id, out _oid))
            {
                return -1;
            }

            return _oid;
        }

        /// <summary>
        /// 获取地址栏Int型参数
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        protected long QueryLong(string name)
        {
            long _oid = -1;
            string _id = Request.QueryString[name];
            if (_id == null || !long.TryParse(_id, out _oid))
            {
                return -1;
            }

            return _oid;
        }

        /// <summary>
        /// 获取地址栏String参数
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        protected string QueryString(string name)
        {
            string result = Request.QueryString[name];
            if (result == null || result.Trim() == "")
            {
                return "";
            }

            return @result.Replace("'", "''");
        }

        /// <summary>
        /// 获取地址栏String参数原型
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        protected string PrototypeQuery(string name)
        {
            string result = Request.QueryString[name];
            if (result == null || result.Trim() == "")
            {
                return "";
            }

            return result;
        }
    }
}
